package edu.ccut.saturn.basecomponent.baseservicecomponent;

import java.io.File;
import java.io.FileOutputStream;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;

public class UploadFile extends DefaultComponent {
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		byte[] content = (byte[]) parameterContext.getParameterValue(0);
		String filePath = (String) parameterContext.getParameterValue(1);
		File file = new File(filePath);
		if (!file.exists()) {
			String path = file.getParent();
			String name = file.getName();
			file = new File(path);
			file.mkdirs();
			file = new File(path + File.separator + name);
		}
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(file);
			out.write(content);
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
}